package com.sia.springcloud.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sia.springcloud.mapper.DeviceMapper;
import com.sia.springcloud.model.Device;
import com.sia.springcloud.result.ResultUtil;
import com.sia.springcloud.service.IDataShowService;
import lombok.extern.slf4j.Slf4j;
import org.ietf.jgss.Oid;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.xml.transform.Result;
import java.util.List;

/**
 * @Author: Sirius
 * @Date: 2022/3/2 14:22
 * @Description: 数据实时数据展示
 **/
@Slf4j
@RestController
@RequestMapping("//dataShowController")
public class DataShowController {


    @Resource
    private IDataShowService dataShowService;

    @Resource
    private DeviceMapper deviceMapper;




    /**
     * 根据协议查询正在进行数采的设备
     *
     * @param protocolName
     * @return
     */
    @RequestMapping("/deviceList")
    public String deviceList(String protocolName) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("protocol_name",protocolName);
        queryWrapper.eq("collect",1);
        List<Device> deviceList = deviceMapper.selectList(queryWrapper);
        if(null!=deviceList&&deviceList.size()>0){
            return  JSON.toJSONString(deviceList);
        }else{
            return null;
        }
    }


    /**
     * 查询设备的数采状态，0表示关闭数采，1表示正在数采
     *
     * @param deviceId
     * @return
     */
    @RequestMapping("/deviceStatus")
    public String deviceStatus(String deviceId) {
        Device device = deviceMapper.selectById(deviceId);
        if (device == null) {
            return ResultUtil.createSimpleFailureJson("设备不存在");
        } else {
            if(device.getCollect()==1){
                return ResultUtil.createSimpleSuccessJson("数采中");
            }else{

                return ResultUtil.createSimpleSuccessJson("未开启数采");
            }
        }

    }

    /**
     * 发送实时数据
     *
     * @param start
     * @param limit
     * @param deviceId
     * @return
     */
    @RequestMapping("/realTimeData")
    public String realTimeData(String start,String limit, String deviceId) {
        log.info("获取设备{}的实时数据", deviceId);

        return dataShowService.showRealTimeData(Integer.parseInt(start),Integer.parseInt(limit), deviceId);
    }


}
